import {getPageAdPicList, handleAdPicClick} from "../../../../utils/util"

Page({
	data: {
		selectFileInfo: {}, // 成绩单/文件缩略图信息
		currentPrintType: {}, // 当前选择的成绩单/文件
		param: {}, // 成绩单/文件二级选项
		// 广告图片
		adImageItem: {
			imgAddress: "",
			placementLocation: "",
			jumpType: "",
			jumpAddress: "",
			appid: "",
		},
	},
	onLoad(option) {
		// 获取广告图片
		getPageAdPicList("workService:pdfImage").then((list) => {
			if (list && list.length > 0) {
				this.setData({
					adImageItem: list[0],
				})
			}
		})
		try {
			const { currentPrintType, paramString } = option
			this.setData({
				currentPrintType: JSON.parse(decodeURIComponent(currentPrintType)),
				param: JSON.parse(decodeURIComponent(paramString)),
			})
		} catch (error) {
			console.log(error)
		}
		this.getPdfPic()
	},
	/**
	 * @see 获取成绩单缩略图
	 */
	getPdfPic() {
		const { currentPrintType, param } = this.data
		const request = Object.assign(
			{
				fileProperty: currentPrintType.vcPrintTypeId, // 文件唯一标识
			},
			param,
		)
		wx.showLoading({
			title: "加载中...",
			mask: true,
		})
		wx.$API["workService/getPdfPic"](request)
			.then((result) => {
				wx.hideLoading()
				if (result.errcode === "1") {
					wx.showToast({
						title: "您没有该类型",
						icon: "none",
					})
					this.setData({
						selectFileInfo: {},
					})
				} else {
					this.setData({
						selectFileInfo: result,
					})
				}
			})
			.catch((err) => {
				wx.hideLoading()
				wx.showToast({
					title: err,
					icon: "none",
				})
			})
	},
	/**
	 * @see 放大预览图片
	 */
	previewImage() {
		wx.previewImage({
			urls: [this.data.selectFileInfo.smallImageList],
		})
	},
	/**
	 * @see 获取成绩单、文件商品信息
	 */
	goSendEmail() {
		const { currentPrintType, selectFileInfo, param } = this.data
		if (!Object.keys(selectFileInfo).length) {
			wx.showToast({
				title: "您没有该类型",
				icon: "none",
			})
			return
		}
		const request = {
			fileProperty: param.filePropertyType, // 文件属性（0:成绩单，1:电子证明）
			fileType: currentPrintType.fileType, // 文件类型（0:中文类型，1:英文类型）
			productName: currentPrintType.printType,
			productNum: 1, // 数量
		}
		wx.showLoading({
			title: "加载中...",
			mask: true,
		})
		wx.$API["workService/getPayProductInfo"](request)
			.then((result) => {
				wx.hideLoading()
				try {
					const orderDetails = JSON.stringify({
						...result,
						fileName: currentPrintType.printType,
						pdfSerialId: selectFileInfo.pdfSerialId,
					})
					let url
					if (param.filePropertyType === 0) {
						url = `/pages/workService/components/sendEmail/index?type=电子成绩单&orderDetails=${orderDetails}`
					} else {
						url = `/pages/workService/components/sendEmail/index?type=电子证明&orderDetails=${orderDetails}`
					}
					wx.redirectTo({
						url,
					})
				} catch (err) {
					console.log(err)
					wx.showToast({
						title: "订单错误，请联系相关管理员",
						icon: "none",
					})
				}
			})
			.catch((err) => {
				wx.hideLoading()
				wx.showToast({
					title: err,
					icon: "none",
				})
			})
	},
	//点击了广告 banner
	advertClick() {
		handleAdPicClick(this.data.adImageItem)
		// wx.navigateToMiniProgram({
		// 	appId: "wxfa108ee7b0593fef",
		// 	path: "pages/index/index",
		// })
	},
	//跳转到小程序的其他页面
	jumpPage(event) {
		wx.navigateTo({
			url: event.currentTarget.dataset.url,
		})
	},
})
